package com.financial.business.integral.service.yd.impl;

import com.financial.business.integral.constants.FinancialPayIntegralEnumConstants;
import com.financial.business.integral.constants.asyn.FinancialPayAsyncEnumConstants;
import com.financial.business.integral.dao.asyn.FinancialPayAsyncDao;
import com.financial.business.integral.domain.asyn.FinancialPayAsyncDO;
import com.financial.business.integral.service.yd.FinancialYDIntegralPayAsynService;
import com.financial.business.integral.service.yd.FinancialYDIntegralPayService;
import com.financial.business.integral.utils.HttpClientUtils;
import com.financial.business.integral.utils.JsonUtils;
import com.financial.business.integral.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //优店积分异步回执接口
 * @Date 2019/12/10 15:40
 */
@Service
public class FinancialYDIntegralPayAsynServiceImpl implements FinancialYDIntegralPayAsynService {
    public static final Logger logger = LoggerFactory.getLogger(FinancialYDIntegralPayAsynServiceImpl.class);

    @Autowired
    private FinancialPayAsyncDao financialPayAsyncDao;

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    private FinancialYDIntegralPayService financialYDIntegralPayService;
    @Override
    public ApiResult payAsyn(Map<String, Object> paramMap) {
        logger.info("FinancialYDIntegralPayAsynServiceImpl.payAsyn---接收优店积分支付异步回执参数map格式:" + paramMap.toString());
        String payAsynJson = JsonUtils.objectToJson(paramMap);
        String orderNum = "";
        /*初始化返回值值*/
        String result = FinancialPayAsyncEnumConstants.ENUM_SEND_FAIL;
        try {
            String balanceCode = paramMap.get("currencyCode").toString();
            String payCurrency = paramMap.get("payCurrency").toString();
            orderNum = paramMap.get("orderNum").toString();
            /*获取平台编号*/
            String registBusNum = paramMap.get("platformBusnum").toString();
            /*对分账所有的金额进行精确度设置，保留两位小数*/
            /*支付积分金额*/
            BigDecimal totalAmount = new BigDecimal(payCurrency).setScale(2, RoundingMode.HALF_UP);
            /*积分预支付异步信息通知优点平台回执记录*/
            FinancialPayAsyncDO financialPayAsyncDO = new FinancialPayAsyncDO();
            financialPayAsyncDO.setBusinessChannel(FinancialPayAsyncEnumConstants.ENUM_BUSINESSCHANNEL_GYBALANCE);
            financialPayAsyncDO.setGmtCreate(new Date());
            financialPayAsyncDO.setGmtModified(new Date());
            financialPayAsyncDO.setIsDelete(FinancialPayAsyncEnumConstants.ENUM_ISDELETE_UNDEL);
            financialPayAsyncDO.setOutTradeNo(orderNum);
            financialPayAsyncDO.setPlatformNo(registBusNum);
            financialPayAsyncDO.setTotalAmount(totalAmount);
            financialPayAsyncDO.setReturnMessage(payAsynJson);
            if (balanceCode.equals(FinancialPayAsyncEnumConstants.ENUM_GYBALANCECALLBANK_SUC)) {
                financialPayAsyncDO.setState(FinancialPayAsyncEnumConstants.ENUM_STATE_SUC);
            } else {
                financialPayAsyncDO.setState(FinancialPayAsyncEnumConstants.ENUM_STATE_FAIL);
            }
            int res = financialPayAsyncDao.save(financialPayAsyncDO);
            if (res > 0) {
                /*获取请求回调地址*/
                Map<String, Object> condMap = new HashMap<>();
                condMap.put("outTradeNo", orderNum);
                condMap.put("state", FinancialPayIntegralEnumConstants.ENUM_STATE_SUC);
                String resqContent = financialYDIntegralPayService.query(condMap);
                logger.info("FinancialYDIntegralPayAsynServiceImpl.payAsyn---优店积分异步回执查询该笔订单的请求内容为!" + resqContent);
                if (resqContent != null && resqContent != "") {
                    /*查询resq_content内容中的notify_url回调地址*/
                    Map<String, Object> resqContentMap = JsonUtils.jsonToMap(resqContent);
                    String notifyUrl = (String) resqContentMap.get("notify_url");
                    logger.info("FinancialYDIntegralPayAsynServiceImpl.payAsyn---优店积分异步回执查询该笔订单的回执地址!" + notifyUrl);
                    /*通知调用中心*/
                    result = httpClientUtils.doPost(notifyUrl, paramMap);
                    logger.info("FinancialYDIntegralPayAsynServiceImpl.payAsyn---优店积分发送成功!");
                    return ApiResult.success(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYDIntegralPayAsynServiceImpl.payAsyn---优店积分回调调用平台失败:" + e.getMessage());
        }
        return ApiResult.failure(result);
    }
}
